{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [Insert Entities](https://milvus.io/docs/v2.3.0-beta/insert_data.md)\n",
    "\n",
    "> This topic describes how to insert data in Milvus via client.\n",
    "\n",
    "Milvus 2.1 supports VARCHAR data type on scalar field. When building indexes for VARCHAR-type scalar fields, the default index type is dictionary tree.\n",
    "\n",
    "The following example inserts 2,000 rows of randomly generated data as the example data. Real applications will likely use much higher dimensional vectors than the example. You can prepare your own data to replace the example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Milvus.Client, 2.2.2-preview.2</span></li></ul></div></div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>{MilvusClient:localhost:19530}</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>Address</td><td><div class=\"dni-plaintext\"><pre>localhost:19530</pre></div></td></tr></tbody></table></div></details><style>\r\n",
       ".dni-code-hint {\r\n",
       "    font-style: italic;\r\n",
       "    overflow: hidden;\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview {\r\n",
       "    white-space: nowrap;\r\n",
       "}\r\n",
       ".dni-treeview td {\r\n",
       "    vertical-align: top;\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "details.dni-treeview {\r\n",
       "    padding-left: 1em;\r\n",
       "}\r\n",
       "table td {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "table tr { \r\n",
       "    vertical-align: top; \r\n",
       "    margin: 0em 0px;\r\n",
       "}\r\n",
       "table tr td pre \r\n",
       "{ \r\n",
       "    vertical-align: top !important; \r\n",
       "    margin: 0em 0px !important;\r\n",
       "} \r\n",
       "table th {\r\n",
       "    text-align: start;\r\n",
       "}\r\n",
       "</style>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#r \"nuget: Milvus.Client, 2.2.2-preview.2\"\n",
    "#!import config/Settings.cs\n",
    "\n",
    "using Milvus.Client;\n",
    "using InteractiveKernel = Microsoft.DotNet.Interactive.Kernel;\n",
    "\n",
    "//Connect to milvus\n",
    "(string endpoint, int port, string userName, string password) = Settings.LoadFromFile();\n",
    "\n",
    "MilvusClient milvusClient = default;\n",
    "\n",
    "milvusClient = new MilvusClient(endpoint, port, userName, password, null);\n",
    "milvusClient"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare data\n",
    "\n",
    "🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱🧱\n",
    "\n",
    "First, prepare the data to insert. Data type of the data to insert must match the schema of the collection, otherwise Milvus will raise exception."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "Random ran = new ();\n",
    "List<long> bookIds = new ();\n",
    "List<long> wordCounts = new ();\n",
    "List<ReadOnlyMemory<float>> bookIntros = new ();\n",
    "List<string> bookNames = new ();\n",
    "for (long i = 0L; i < 2000; ++i)\n",
    "{\n",
    "    bookIds.Add(i);\n",
    "    wordCounts.Add(i + 10000);\n",
    "    bookNames.Add($\"Book Name {i}\");\n",
    "\n",
    "    float[] vector = new float[2];\n",
    "    for (int k = 0; k < 2; ++k)\n",
    "    {\n",
    "        vector[k] = ran.Next();\n",
    "    }\n",
    "    bookIntros.Add(vector);\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Insert data to Milvus\n",
    "\n",
    "| Parameter | Description |\n",
    "| --- | --- |\n",
    "| fieldName | Name of the field to insert data into. |\n",
    "| DataType | Data type of the field to insert data into. |\n",
    "| data | Data to insert into each field. |\n",
    "| CollectionName | Name of the collection to insert data into. |\n",
    "| PartitionName (optional) | Name of the partition to insert data into. |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Insert count:2000,\r\n"
     ]
    }
   ],
   "source": [
    "string collectionName = \"book\";\n",
    "string partitionName = \"\";// \"novel\";// Donnot Use partition name when you are connecting milvus hosted by zilliz cloud.\n",
    "\n",
    "MilvusCollection collection = milvusClient.GetCollection(collectionName);\n",
    "\n",
    "MutationResult result = await collection.InsertAsync(\n",
    "    new FieldData[]\n",
    "    {\n",
    "        FieldData.Create(\"book_id\", bookIds),\n",
    "        FieldData.Create(\"word_count\", wordCounts),\n",
    "        FieldData.Create(\"book_name\", bookNames),\n",
    "        FieldData.CreateFloatVector(\"book_intro\", bookIntros),\n",
    "    },\n",
    "    partitionName);\n",
    "\n",
    "// Check result\n",
    "Console.WriteLine(\"Insert count:{0},\", result.InsertCount);"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
